package com.xiaoqian.partyembassy.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.xiaoqian.partyembassy.R;
import com.xiaoqian.partyembassy.constant.AppConfig;
import com.xiaoqian.partyembassy.tools.SharedPreUtils;
import com.xiaoqian.partyembassy.view.DialogLoadingView;
import java.util.concurrent.TimeUnit;
import androidx.annotation.NonNull;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;


/**
 *
 */
public abstract class BaseFragment extends Fragment {
	/**
	 * 上下文
	 */
	protected Context mContext;
	/**
	 * activity
	 */
	protected Activity mActivity;
	/**
	 * 进度条
	 */
	private DialogLoadingView mLoadingViewDialog;
	/**
	 * 对话框
	 */
	private Toast mToast;
	/**
	 * 判断是否初始化数据
	 */
	public boolean isInitData;
	protected View view;

	//被观察者

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mContext = activity;
		this.mActivity = activity;
	}

	/**
	 * 构造函数
	 */
	public BaseFragment() {
		super();
	}

	public BaseFragment(Activity mActivitys) {
		this.mActivity = mActivitys;
	}


	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		view = inflater.inflate(getLayoutId(), container, false);
		isInitData = true;
		mLoadingViewDialog = new DialogLoadingView(mContext);

		return view;
	}

	/**
	 * 获取布局
	 *
	 * @return
	 */
	protected abstract int getLayoutId();

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		if (getUserVisibleHint()) {
			showData();

		}
	}



	/**
	 * 加载数据
	 */
	protected void showData() {
		if (isInitData) {
			isInitData = false;
			initParams();
			initView();
		}
	}

	/**
	 * 加载数据
	 */
	protected abstract void initParams();

	/**
	 * 初始化控件
	 */
	protected abstract void initView();



	/**
	 * 显示进度条
	 */
	public void ShowLoaingViewDialog() {
		try {
			if (mLoadingViewDialog != null && !mLoadingViewDialog.isShowing()) {
				mLoadingViewDialog.show();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	/**
	 * 取消进度条
	 */
	public void disLoadingViewDialog() {
		if (mLoadingViewDialog != null && mLoadingViewDialog.isShowing()) {
			mLoadingViewDialog.dismiss();
		}
	}

	/**
	 * 显示对话框
	 *
	 * @param text
	 */
	public void showToast(String text) {
		if (mToast == null) {
			mToast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
		} else {
			mToast.setText(text);
			mToast.setDuration(Toast.LENGTH_SHORT);
		}
		mToast.show();
	}
	/**
	 * 显示对话框
	 *
	 * @param text
	 */
	public void showLongToast(String text) {
		if (mToast == null) {
			mToast = Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT);
		} else {
			mToast.setText(text);
			mToast.setDuration(Toast.LENGTH_LONG);
		}
		mToast.show();
	}
	/**
	 * 清空Toast
	 */
	public void cancelToast() {
		if (mToast != null) {
			mToast.cancel();
		}
	}
	/**
	 * 获取Long
	 *
	 * @param keyword
	 *            键
	 * @return 值（默认为0）
	 */
	protected long getLongShareValue(String keyword) {
		return SharedPreUtils.getLong(AppConfig.SAVEPATHNAME, keyword);
	}
	/**
	 * 保存Long
	 *
	 * @param id
	 *            键
	 * @param value
	 *            值
	 */
	protected void  setLongShareValue(String id, long value) {
		SharedPreUtils.setLong(value, AppConfig.SAVEPATHNAME, id);
	}
	/**
	 * 保存字符串
	 *
	 * @param id
	 * @param value
	 */
	protected boolean setStringShareValue(String id, String value) {
		return SharedPreUtils.setString(value, AppConfig.SAVEPATHNAME, id);
	}

	/**
	 * 得到保存的字符串
	 *
	 * @param keyword
	 * @return
	 */
	protected String getStringShareValue(String keyword) {
		return SharedPreUtils.getString(AppConfig.SAVEPATHNAME, keyword);
	}

	/**
	 * 保存Int
	 *
	 * @param id
	 * @param value
	 */
	protected void setIntShareValue(String id, int value) {
		SharedPreUtils.setInt(value, AppConfig.SAVEPATHNAME, id);
	}

	/**
	 * 获取Int
	 *
	 * @param keyword
	 * @return
	 */
	protected int getIntShareValue(String keyword) {
		return SharedPreUtils.getInt(AppConfig.SAVEPATHNAME, keyword);

	}

	/**
	 * 保存boolean
	 */
	protected void setBooleanShareValue(String id, boolean value) {
		SharedPreUtils.setBoolean(value, AppConfig.SAVEPATHNAME, id);
	}

	/**
	 * 获取boolean
	 */
	protected boolean getBooleanShareValue(String id) {
		return SharedPreUtils.getBoolean(false, AppConfig.SAVEPATHNAME, id);

	}

	/**
	 * 清除所有数据
	 */
	protected void cleanAllData() {
		SharedPreUtils.clearAllData(AppConfig.SAVEPATHNAME);
	}

	/** 不含参数的Class跳转 **/
	protected void startActivity(Class<?> cla) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cla);
		startActivity(intent);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}
	}

	/** 不含参数的Class跳转,有请求码 **/
	protected void startActivityWithCode(Class<?> cla, int requestCode) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cla);
		startActivityForResult(intent, requestCode);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}
	}

	/** 不含参数的Class跳转,关闭本界面 **/
	protected void startActivityAndFinish(Class<?> cla) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cla);
		startActivity(intent);
		mActivity.finish();
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}

	}

	/** 含有Bundle通过Class跳转界面 **/
	protected void startActivity(Class<?> cls, Bundle bundle) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivity(intent);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}
	}

	/** 含有Bundle通过Class跳转界面,有请求码 **/
	protected void startActivityWithCode(Class<?> cls, Bundle bundle,
                                         int requestCode) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cls);
		if (bundle != null) {
			intent.putExtras(bundle);
		}
		startActivityForResult(intent, requestCode);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}
	}

	protected void startActivityWithIntent(Class<?> cls, Intent intent) {
		if (intent != null) {
			intent.setClass(mContext, cls);
		}
		startActivity(intent);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}
	}

	/** 含有Bundle通过Class跳转界面,关闭界面 **/
	protected void startActivityAndFinish(Class<?> cls, Bundle bundle) {
		if(null != mActivity){
			Intent intent = new Intent();
			intent.setClass(mActivity, cls);
			if (bundle != null) {
				intent.putExtras(bundle);
			}
			startActivity(intent);
			mActivity.finish();
			if (isAdded()) {
				mActivity.overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}

		}
	}

	/** 含有String通过Class跳转界面,有请求码 **/
	protected void startActivityWithCode(Class<?> cls, String key,
                                         String value, int requestCode) {
		if(null != mActivity){
			Intent intent = new Intent();
			intent.setClass(mActivity, cls);
			intent.putExtra(key, value);
			startActivityForResult(intent, requestCode);
			if (isAdded()) {
				mActivity.overridePendingTransition(R.anim.push_left_in,
						R.anim.push_left_out);
			}

		}

	}

	/** 含有String通过Class跳转界面, **/
	protected void startActivity(Class<?> cls, String key, String value) {
		Intent intent = new Intent();
		intent.setClass(mActivity, cls);
		intent.putExtra(key, value);
		startActivity(intent);
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.push_left_in,
					R.anim.push_left_out);
		}

	}

	/** 带有右进左出的动画退出 **/
	public void finish() {
		mActivity.finish();
		if (isAdded()) {
			mActivity.overridePendingTransition(R.anim.back_left_in,
					R.anim.back_right_out);
		}

	}

	/** 默认退出 **/
	public void defaultFinish() {
		mActivity.finish();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mLoadingViewDialog != null && mLoadingViewDialog.isShowing()) {
			mLoadingViewDialog.dismiss();
			mLoadingViewDialog = null;
		}

	}



	@Override
	public void onDestroyView() {
		super.onDestroyView();
		disLoadingViewDialog();
	}
	private Disposable mDisposable;
	protected void countDown(int time){
		cancel();
		Observable.timer(time, TimeUnit.SECONDS)
				.observeOn(AndroidSchedulers.mainThread())
				.subscribe(new Observer<Long>() {
					@Override
					public void onSubscribe(@NonNull Disposable disposable) {

						mDisposable=disposable;
					}

					@Override
					public void onNext(@NonNull Long number) {
					}

					@Override
					public void onError(@NonNull Throwable e) {
						//取消订阅
						cancel();
					}

					@Override
					public void onComplete() {
						//取消订阅
						dissmissShow();
						cancel();
					}
				});
	}
	/**
	 * 取消订阅
	 */
	public  void cancel(){
		if(mDisposable!=null&&!mDisposable.isDisposed()){
			mDisposable.dispose();
		}
	}
	protected void dissmissShow(){}
}
